<?php
class BlogController extends Controller
{
    public $models = 'Blog,Tag';

    public function add()
    {
        global $PARAM, $ENV;
        if (empty($PARAM['blogContent']))
            redirect('index');
        $sep = array(',', ';', ' ', '，', '；', '。', '.');
        $tags = str_replace($sep, ',', $PARAM['blogTag']);
        $tags = arrayTrim(explode(',', $tags));
        if (!is_array($tags))
            return 0;
        $tagIds = array();
        foreach ($tags as $tag){
            $tagIds[] = $this->Tag->addNewTag(htmlspecialchars($tag));
        }
        $this->Blog->addBlog(htmlspecialchars($PARAM['blogContent']), $tagIds);
        redirect('index');
    }

    public function del()
    {
        global $PARAM;
        if (!isset($PARAM['id']))
            die();
        $id = (int)$PARAM['id'];
        $b = $this->Blog->find($id);
        if ($b){
            $tagIds = arrayTrim(explode('#', $b['tags']));
            if (!empty($tagIds))
                foreach ($tagIds as $tid){
                    $this->Tag->decreaseCount($tid);
                }
            if ($this->Blog->delete($id))
                echo 'OK';
        }
    }
    public function mark()
    {
        global $PARAM;
        if (!isset($PARAM['id']))
            die();
        $id = (int)$PARAM['id'];
        $set = ($PARAM['set'] == 'false') ? 0 : 1;
        $b = $this->Blog->find($id);
        if ($b){
            if ($this->Blog->setMark($id, $set))
                echo 'OK';
        }
    }
}
?>
